package me.zhengjie.org.centerbase.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.org.centerbase.domain.CenterBase;
import me.zhengjie.org.centerbase.service.CenterBaseService;
import me.zhengjie.org.centerbase.service.dto.CenterBaseQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Api(tags = "中心基础信息管理")
@RestController
@RequestMapping("/api/centerBase")
public class CenterBaseController {

    private final CenterBaseService centerBaseService;

    public CenterBaseController(CenterBaseService centerBaseService) {
        this.centerBaseService = centerBaseService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('centerBase:list')")
    public void download(HttpServletResponse response, CenterBaseQueryCriteria criteria) throws IOException {
        centerBaseService.download(centerBaseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询中心基础信息")
    @ApiOperation("查询中心基础信息")
    @PreAuthorize("@el.check('centerBase:list')")
    public ResponseEntity<Object> getCenterBases(CenterBaseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(centerBaseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增中心基础信息")
    @ApiOperation("新增中心基础信息")
    @PreAuthorize("@el.check('centerBase:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CenterBase resources){
        return new ResponseEntity<>(centerBaseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改中心基础信息")
    @ApiOperation("修改中心基础信息")
    @PreAuthorize("@el.check('centerBase:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CenterBase resources){
        centerBaseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除中心基础信息")
    @ApiOperation("删除中心基础信息")
    @PreAuthorize("@el.check('centerBase:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        centerBaseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}